#define _CRT_SECURE_NO_WARNINGS 

#include "Employee_management.h"

void menu()
{
	printf("\n\n\n");
	printf("\t\t-------------------------------------------------\n");
	printf("\t\t||              ----------------               ||\n");
	printf("\t\t||**************职工信息管理系统***************||\n");
	printf("\t\t||              ----------------               ||\n");
	printf("\t\t||                                             ||\n");
	printf("\t\t||~~~~~~~~~~~~~~~1.录入职工信息~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||~~~~~~~~~~~~~~~2.删除职工信息~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||~~~~~~~~~~~~~~~3.修改职工信息~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||~~~~~~~~~~~~~~~4.查询职工信息~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||~~~~~~~~~~~~~~~5.显示职工信息~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||~~~~~~~~~~~~~~~6.排序职工信息~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||~~~~~~~~~~~~~~~7.输入到文件~~~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||~~~~~~~~~~~~~~~8.退出系统~~~~~~~~~~~~~~~~~~~~||\n");
	printf("\t\t||                                             ||\n");
	printf("\t\t||*********************************************||\n");
}

void Init_List(PLinear_List& PL)
{
	PL = new Linear_List;
	PL->capacity = 4;
	PL->size = 0;
	PL->data = new Employee[4];//(PEmployee)malloc(sizeof(Employee) * PL->capacity); 
}

void Destroy(PLinear_List& PL)
{
	PL->size = 0;
	PL->capacity = 0;
	delete PL->data;
	delete PL;
}

void Increase_capacity(PLinear_List& PL)
{
	PL->capacity *= 2;

	PEmployee newdata = (PEmployee)realloc(PL->data, PL->capacity * sizeof(Employee));
	assert(newdata);

	PL->data = newdata;
}

void Push_Back(PLinear_List& PL)
{
	if (PL->size == PL->capacity)
	{
		//扩容
		Increase_capacity(PL);
	}
	PEmployee PE = new Employee;
	//输入数据
	Input(PE);
	//按照姓名字典序插入
	//计算插入的位置
	int pos = 0;
	for (pos = 0; pos < PL->size; pos++)
	{
		if (strcmp(PE->name, PL->data[pos].name) < 0)
			break;
	}
	//将pos位置后面的数据右移
	int i = PL->size - 1;
	while (i >= pos)
	{
		PL->data[i + 1] = PL->data[i];
		i--;
	}
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

	printf("工号\t  |姓名\t  |性别\t|年龄 |出生日期\t\t|工作日期\t\t|学历\t|职务\t     |住址\t\t\t|电话\t\t\t|\n");
	PrintOne(PE);
	PL->data[pos] = *PE;
	PL->size++;
	delete PE;

	cout << "录入成功" << endl;
}

void Delete(PLinear_List& PL)
{
	//根据职工编号或者姓名删除职工信息
	cout << "1.根据姓名查找该员工" << endl;
	cout << "2.根据工号查找该员工" << endl;
	int x;
	cin >> x;
	int pos = 0;
	//找到要删除职工的下标
	if (x == 1)
	{
		cout << "请输入姓名" << endl;
		char fname[20];
		cin >> fname;
		for (pos = 0; pos < PL->size; pos++)
		{
			if (strcmp(fname, PL->data[pos].name) == 0)
				break;
		}
	}
	else
	{
		cout << "请输入工号" << endl;
		char fnumber[20];
		cin >> fnumber;
		for (pos = 0; pos < PL->size; pos++)
		{
			if (strcmp(fnumber, PL->data[pos].number) == 0)
				break;
		}
	}
	//向前覆盖，size--
	PL->size--;
	for (int i = pos; pos < PL->size; pos++)
	{
		PL->data[i] = PL->data[i + 1];
	}

	cout << "删除成功" << endl;
}

void Input(PEmployee& PE)
{
	cout << "请输入要插入的职工的信息" << endl;

	cout << "职工编号: ";
	cin >> PE->number;

	cout << "姓名: ";
	cin >> PE->name;

	cout << "性别: ";
	cin >> PE->sex;

	cout << "年龄: ";
	cin >> PE->age;

	cout << "出生年月: ";
	cin >> PE->date_of_birth;

	cout << "参与工作年月: ";
	cin >> PE->date_work;

	cout << "学历: ";
	cin >> PE->educational_backgrounf;;

	cout << "职务: ";
	cin >> PE->job;

	cout << "住址: ";
	cin >> PE->address;

	cout << "电话: ";
	cin >> PE->tele_num;
}


PEmployee Findname(PLinear_List& PL)
{
	cout << "请输入要查找的职工的姓名：";
	char fname[20];
	cin >> fname;
	for (int i = 0; i < PL->size; i++)
	{
		if (strcmp(PL->data[i].name, fname) == 0)
		{
			return &(PL->data[i]);
		}
	}
	cout << "输入错误，没有该员工" << endl;
	return nullptr;
}

PEmployee Findnumber(PLinear_List& PL)
{
	cout << "请输入要查找的职工的工号：";
	char fnumber[20];
	cin >> fnumber;
	for (int i = 0; i < PL->size; i++)
	{
		if (strcmp(PL->data[i].number, fnumber) == 0)
		{
			return &(PL->data[i]);
		}
	}
	cout << "输入错误，没有该员工" << endl;
	return nullptr;
}

void Findjob(PLinear_List& PL)
{
	cout << "请输入要查找的职工的工作：";
	char fjob[20];
	cin >> fjob;
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

	printf("工号\t  |姓名\t  |性别\t|年龄 |出生日期\t\t|工作日期\t\t|学历\t|职务\t     |住址\t\t\t|电话\t\t\t|\n");


	for (int i = 0; i < PL->size; i++)
	{
		if (strcmp(PL->data[i].job, fjob) == 0)
		{
			//输出该类职工信息
			PEmployee PE = &(PL->data[i]);
			PrintOne(PE);
		}
	}
}

void Finded_b(PLinear_List& PL)
{
	cout << "请输入要查找的职工的学历：";
	char fed_b[20];
	cin >> fed_b;
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

	printf("工号\t  |姓名\t  |性别\t|年龄 |出生日期\t\t|工作日期\t\t|学历\t|职务\t     |住址\t\t\t|电话\t\t\t|\n");

	for (int i = 0; i < PL->size; i++)
	{
		if (strcmp(PL->data[i].educational_backgrounf, fed_b) == 0)
		{
			//输出该职工信息
			PEmployee PE = &(PL->data[i]);
			PrintOne(PE);
		}
	}
}

void Findage(PLinear_List& PL)
{
	cout << "请输入要查找的职工的年龄：";
	int fage;
	cin >> fage;
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

	printf("工号\t  |姓名\t  |性别\t|年龄 |出生日期\t\t|工作日期\t\t|学历\t|职务\t     |住址\t\t\t|电话\t\t\t|\n");

	for (int i = 0; i < PL->size; i++)
	{
		if (PL->data[i].age == fage)
		{
			//打印信息
			PEmployee PE = &(PL->data[i]);
			PrintOne(PE);
		}
	}
}

void Find(PLinear_List& PL)
{
	int x;

	printf("…………1.根据姓名查找…………\n");
	printf("…………2.根据工号查找…………\n");
	printf("…………3.根据职务查找…………\n");
	printf("…………4.根据学历查找…………\n");
	printf("…………5.根据年龄查找…………\n");
	cin >> x;
	PEmployee cur;
	switch (x)
	{
	case 1:
		cur = Findname(PL);
		if (cur)
		{
			//输出该职工的信息
			PrintOne(cur);
		}
		break;
	case 2:
		cur = Findnumber(PL);
		if (cur)
		{
			//输出该职工的信息
			PrintOne(cur);
		}
		break;
	case 3:
		Findjob(PL);
		break;
	case 4:
		Finded_b(PL);
		break;
	case 5:
		Findage(PL);
		break;
	}
}

//排序
void Sort(PLinear_List& PL)
{
	int n = PL->size;
	//冒泡排序实现
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - i - 1; j++)
		{
			if (strcmp(PL->data[j].number, PL->data[j + 1].number) > 0)
				swap(PL->data[j], PL->data[j + 1]);
		}
	}

	cout << "排序成功" << endl;
}


void Save(PLinear_List& PL)
{
	FILE* fp;
	int n = PL->size;
	char filename[100];
	cout << "请输入要保存的文件名: " << endl;
	cin >> filename;
	//断言
	assert(fp = fopen(filename, "w"));
	fprintf(fp, "工号\t\t\t姓名\t\t\t性别\t\t\t年龄\t\t\t-出生日期\t\t\t-工作日期\t\t\t学历\t\t\t职务\t\t\t住址\t\t\t电话\n");
	for (int i = 0; i < n; i++)
		fprintf(fp, "%s\t\t\t%s\t\t\t%s\t\t\t%d\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\n", PL->data[i].number, PL->data[i].name, PL->data[i].sex,
			PL->data[i].age, PL->data[i].date_of_birth, PL->data[i].date_work,
			PL->data[i].educational_backgrounf, PL->data[i].job, PL->data[i].address, PL->data[i].tele_num);
	cout << "保存成功" << endl;
	fclose(fp);

}

void Print(PLinear_List& PL)
{
	int n = PL->size;
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

	printf("工号\t  |姓名\t  |性别\t|年龄 |出生日期\t\t|工作日期\t\t|学历\t|职务\t     |住址\t\t\t|电话\t\t\t|\n");
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

	for (int i = 0; i < n; i++) {
		printf("%-10s|%-7s|%-5s|%-5d|%-17s|%-23s|%-7s|%-12s|%-25s|%-24s|\n", PL->data[i].number, PL->data[i].name, PL->data[i].sex,
			PL->data[i].age, PL->data[i].date_of_birth, PL->data[i].date_work,
			PL->data[i].educational_backgrounf, PL->data[i].job, PL->data[i].address, PL->data[i].tele_num);
		printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

	}
}


void PrintOne(PEmployee& PE)
{
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");
	printf("%-10s|%-7s|%-5s|%-5d|%-17s|%-23s|%-7s|%-12s|%-25s|%-24s|\n", PE->number, PE->name, PE->sex,
		PE->age, PE->date_of_birth, PE->date_work,
		PE->educational_backgrounf, PE->job, PE->address, PE->tele_num);
	printf("-------------------------------------------------------------------------------------------------------------------------------------------------\n");

}


//修改职工信息

void Modifi(PLinear_List& PL)
{
	//先找到要修改的职工， 通过工号和姓名进行型查找
	cout << "1.根据姓名查找该员工" << endl;
	cout << "2.根据工号查找该员工" << endl;
	int x;
	cin >> x;
	PEmployee cur;
	if (x == 1)
	{
		cur = Findname(PL);
		PrintOne(cur);
	}
	else
	{
		cur = Findnumber(PL);
		PrintOne(cur);
	}
	//选择要修改的值
	printf("…………1.修改年龄…………\n");
	printf("…………2.修改学历…………\n");
	printf("…………3.修改住址…………\n");
	printf("…………4.修改职务…………\n");
	printf("…………5.修改电话…………\n");
	cin >> x;
	switch (x)
	{
	case 1:
		cout << "年龄修改为：";
		cin >> cur->age;
		break;
	case 2:
		cout << "学历修改为：";
		cin >> cur->educational_backgrounf;
		break;
	case 3:
		cout << "住址修改为：";
		cin >> cur->address;
		break;
	case 4:
		cout << "职务修改为：";
		cin >> cur->job;
		break;
	case 5:
		cout << "电话修改为：";
		cin >> cur->tele_num;
		break;
	}
	cout << "修改成功" << endl;
}